$(function () {
    //表格
    var payroll_datagrid = $("#payroll_datagrid");
    //表单
    var payroll_add_form = $("#payroll_add_form");
    //弹窗
    var payroll_dialog = $("#payroll_dialog");
    //表格上传
    var payroll_import = $("#payroll_import");
    payroll_datagrid.datagrid({
        url: "/payroll/list",
        columns: [[
            {field: 'eid', title: '员工编号', width: 100,align: 'center',
                formatter: function (value, row, index) {
                 return row.employee ? (row.employee.id ? row.employee.id : "") : "";
                 }},
            {field: 'month', title: '月份', width: 100,align: 'center',formatter: formatDateboxMonth},
            {field: 'employee', title: '员工姓名', width: 100, align: 'center',
                formatter: function (value, row, index) {
                    return value ? value.realName : "";
                }},
            {field: 'baseSalary', title: '基本工资', width: 100,align: 'center'},
            {field: 'workDays', title: '工作天数', width: 100,align: 'center'},
            {field: 'afterDays', title: '迟到天数', width: 100,align: 'center'},
            {field: 'beforeDays', title: '早退天数', width: 100,align: 'center'},
            {field: 'upDays', title: '补签天数', width: 100,align: 'center'},
            {field: 'finalSalary', title: '最终工资', width: 100,align: 'center'},
            {field: 'payTime', title: '结算时间', width: 100,align: 'center',formatter: formatDatebox}
        ]],
        fit: true,
        fitColumns: true,
        border: false,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        toolbar: "#payroll_datagrid_toolbar"
    });
    payroll_dialog.dialog({
        title: "更新",
        //width: 300,
        //height: 300,
        buttons: "#payroll_dialog_buttons",
        closed: true
    });
    //文件上传的弹窗
    payroll_import.dialog({
        title:"文件上传",
        width:400,
        height:150,
        closed:true
    });
    //把方法绑定到一个对象上
    var cmdObj = {
        //员工添加
        addPayroll: function () {
            payroll_add_form.form("clear");
            payroll_dialog.dialog("setTitle", "添加");
            payroll_dialog.dialog("open");
        }, //编辑
        editPayroll: function () {
            //获取选中的一行
            var selectedRow = payroll_datagrid.datagrid("getSelected");
            if (!selectedRow) {
                $.messager.alert("温馨提示", "大哥,你倒是选一行啊!", "error");
                return;
            }
            //清空表单数据
            payroll_add_form.form("clear");
            //显示员工姓名
            $("#employee_realname").html(selectedRow.employee.realName);
            //回显
            payroll_add_form.form("load", selectedRow);
            payroll_dialog.dialog("setTitle", "编辑");
            payroll_dialog.dialog("open");
        }, //删除
        removePayroll: function () {
            //获取选中的一行
            var selectedRow = payroll_datagrid.datagrid("getSelected");
            if (!selectedRow) {
                $.messager.alert("温馨提示", "请选择一行!", "error");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要删除吗", function (yes) {
                if (yes) {
                    $.post("/payroll/delete", {id: selectedRow.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                cmdObj.reloadPayroll();
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "error");
                        }
                    });
                }
            });
        },
        //刷新
        reloadPayroll: function () {
            payroll_datagrid.datagrid("reload");
        },
        save: function () {
            var url = "/payroll/update";
            payroll_add_form.form("submit", {
                url: url,
                success: function (data) {
                    console.log(data);
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", "操作成功", "info", function () {
                            cmdObj.cancel();
                            cmdObj.reloadPayroll();
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                }
            });
        },
        cancel: function () {
            payroll_dialog.dialog("close");
        },//高级查询
        search_realname:function () {
            //获取查询input里面的值
            var keyword = $("#keyword").val();
            //调用datagrid重新加载数据,并且带上查询条件  keyword
            payroll_datagrid.datagrid("load",{keyword:keyword});
        },//结算
        adjust:function () {
            //获取选中的一行
            var selectedRow = payroll_datagrid.datagrid("getSelected");
            if (!selectedRow) {
                $.messager.alert("温馨提示", "请选择一行!", "error");
                return;
            }
            if(selectedRow.payTime){
                $.messager.alert("温馨提示","已经完成结算的不再结算","info");
                return;
            }
            if(selectedRow.workDays == 0){
                $.messager.alert("温馨提示","一个月没上班,不予结算!","info");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要结算吗", function (yes) {
                if (yes) {
                    $.post("/payroll/adjust", {id: selectedRow.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                cmdObj.reloadPayroll();
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "error");
                        }
                    });
                }
            });
        },
        //表格导出
        export:function () {
            window.location.href="/payroll/export";
        },
        //工资表上传
        import:function () {

            $("#payroll_import").dialog("open");
        },
        //模板下载
        download:function () {
            window.location.href="/payroll/download";
        },
        //表格提交/工资表上传
        upload:function () {
            var files = $("#payrolle_xls").filebox("getValue");
            if (!files) {
                $.messager.alert("温馨提示", "请上传工资表", "info");
            }
            $("#import_form").form("submit", {
                url: "/payroll/importXls",
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", "导入成功!", "info", function () {
                            cmdObj.cancel();
                            cmdObj.reloadPayroll();
                            payroll_import.dialog("close");
                        });
                    } else {
                        $.messager.alert("温馨提示", "导入失败!", "error");
                    }
                }
            })
        }
    }
    //2.按钮的点击事件通过js来绑定
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]() ;
    });
});
//时间格式化
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, // month
        "d+": this.getDate(), // day
        "h+": this.getHours(), // hour
        "m+": this.getMinutes(), // minute
        "s+": this.getSeconds(), // second
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
        "S": this.getMilliseconds()
        // millisecond
    }
    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
function formatDatebox(value) {
    if (value == null || value == '') {
        return '';
    }
    var dt;
    if (value instanceof Date) {
        dt = value;
    } else {
        dt = new Date(value);
    }
    return dt.format("yyyy-MM-dd hh:mm:ss");
}
//月份
function formatDateboxMonth(value) {
    if (value == null || value == '') {
        return '';
    }
    var dt;
    if (value instanceof Date) {
        dt = value;
    } else {
        dt = new Date(value);
    }
    return dt.format("yyyy-MM");
}